<template>
  <!-- 逻辑：在这个页面获取数据，通过props传参的方式给各个组件传参 -->
  <div class="report">
    <tableTool>
      <div slot="export">
        <el-button type="primary" @click="excelExport">导出</el-button>
      </div>
      <div class="tool-form" slot="form">
        <div class="input-group">
          <span class="lable">设备名称：</span>
          <el-input placeholder="请输入" v-model="filterEquipmentName" />
        </div>
        <div class="input-group">
          <span class="lable">设备类别：</span>
          <el-input placeholder="请输入" v-model="filterCategoryName" />
        </div>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button type="success" @click="onReset">重置</el-button>
      </div>

    </tableTool>
    <customTable :list="list" :combined="combined">
      <template v-slot:title>
        <tr>
          <th colspan="13">{{ orgName }}</th>
        </tr>
        <tr>
          <th rowspan="2">设备类别</th>
          <th rowspan="2">设备编号</th>
          <th rowspan="2">设备名称</th>
          <th rowspan="2">规格型号</th>
          <th rowspan="2">甲供金额</th>
          <th rowspan="2">甲供成本</th>
          <th rowspan="2">非甲供成本</th>
          <th rowspan="2">超耗扣款</th>
          <th rowspan="2">应扣金额</th>
          <th rowspan="2">扣款金额</th>
          <th rowspan="2">未扣款金额</th>
          <th colspan="2">凭证号</th>
        </tr>
        <tr>
          <th>消耗</th>
          <th>扣款</th>
        </tr>
      </template>
      <template #empty>
        <tr>
          <td colspan="13" class="empty">暂无数据</td>
        </tr>
      </template>
      <template #body="{ list }">
        <tr v-for="item in list" :key="item.id">
          <td>{{ item.equipmentType }}</td>
          <td>{{ item.equipmentNumber }}</td>
          <td>{{ item.equipmentName }}</td>
          <td>{{ item.specificationModel }}</td>
          <td>{{ item.firstPartyAmount }}</td>
          <td>{{ item.firstPartyCost }}</td>
          <td>{{ item.noFirstPartyCost }}</td>
          <td>{{ item.consumeDeduction }}</td>
          <td>{{ item.shouldDeduction }}</td>
          <td>{{ item.deduction }}</td>
          <td>{{ item.noDeduction }}</td>
          <td>{{ item.consumeNo }}</td>
          <td>{{ item.deductionNo }}</td>
        </tr>
      </template>
      <template #footer="{ combined }">
        <tr>
          <td>合计</td>
          <td>{{ combined.equipmentNumber }}</td>
          <td>{{ combined.equipmentName }}</td>
          <td>{{ combined.specificationModel }}</td>
          <td>{{ combined.firstPartyAmount }}</td>
          <td>{{ combined.firstPartyCost }}</td>
          <td>{{ combined.noFirstPartyCost }}</td>
          <td>{{ combined.consumeDeduction }}</td>
          <td>{{ combined.shouldDeduction }}</td>
          <td>{{ combined.deduction }}</td>
          <td>{{ combined.noDeduction }}</td>
          <td>{{ combined.consumeNo }}</td>
          <td>{{ combined.deductionNo }}</td>
        </tr>
      </template>
    </customTable>
    <pagination @onCurrentChange="handleCurrentChange" :total="page.total"></pagination>
  </div>
</template>
<script>
import tableTool from '../components/tableTool.vue'
import customTable from '../components/customTable.vue'
import pagination from '../components/pagination.vue'
import { combinedFn } from '@/pages/comprehensiveReport/equipmentManagement/mixin'
import request from '@/api/comprehensiveReport/equipmentManagement'
export default {
  name: 'equipmentConsumptionDeductionLedgerSubcontractor',
  components: { tableTool, customTable, pagination },
  data () {
    return {
      list: [],
      orgName: '',
      page: {
        total: 0,
        currentPage: 1,
        limit: 10,
      },
      combined: {},
      headers: {
        // 定义对应关系- 把英文的key转化成中文的key所对应的值
        设备类别: 'equipmentType',
        设备编号: 'equipmentNumber',
        设备名称: 'equipmentName',
        规格型号: 'specificationModel',
        甲供金额: 'firstPartyAmount',
        甲供成本: 'firstPartyCost',
        非甲供成本: 'noFirstPartyCost',
        超耗扣款: 'consumeDeduction',
        应扣金额: 'shouldDeduction',
        扣款金额: 'deduction',
        未扣款金额: 'noDeduction',
        消耗: 'consumeNo',
        扣款: 'deductionNo',
      },
      filterCategoryName: '',
      filterEquipmentName: ''
    }
  },
  created () {
    this.getList()
  },
  methods: {
    onSearch () {
      this.getList()
    },
    onReset () {
      this.filterCategoryName = ''
      this.filterEquipmentName = ''
      this.getList()
    },
    async getList () {
      const settleUnit = this.$route.query.settleUnit
      this.orgName = settleUnit
      // 携带参数：currentPage(当前页)，limit(每页显示多少条)
      const data = await request.getEquipmentConsumeSubDetails({
        page: this.page.currentPage,
        limit: this.page.limit,
        settleUnit,
        filterCategoryName: this.filterCategoryName,
        filterEquipmentName: this.filterEquipmentName
      })
      console.log('发送请求，获取数据', data)
      if (data.list.length == 0) {
        this.list = []
        this.combined = {}
        this.page.total = 0
        return;
      }
      this.page.total = data.totalCount
      const { list, combined } = combinedFn(Object.values(this.headers).slice(1), data.list, [
        'firstPartyAmount',
        'firstPartyCost',
        'noFirstPartyCost',
        'consumeDeduction',
        'shouldDeduction',
        'deduction',
        'noDeduction',
        'consumeNo',
        'deductionNo',
      ])
      this.list = list
      this.combined = combined
    },
    handleCurrentChange (currentPage) {
      this.page.currentPage = currentPage
      this.getList()
    },
    excelExport () {
      const multiHeader = [
        [this.orgName, '', '', '', '', '', '', '', '', '', '', '', ''],
        [
          '设备类别',
          '设备编号',
          '设备名称',
          '规格型号',
          '甲供金额',
          '甲供成本',
          '非甲供成本',
          '超耗扣款',
          '应扣金额',
          '扣款金额',
          '未扣款金额',
          '凭证号',
          '',
        ],
      ] // 它是一个二维数组
      const merges = [
        'A1:M1',
        'L2:M2',
        'A2:A3',
        'B2:B3',
        'C2:C3',
        'D2:D3',
        'E2:E3',
        'F2:F3',
        'G2:G3',
        'H2:H3',
        'I2:I3',
        'J2:J3',
        'K2:K3',
      ]
      // 懒加载 script-loader
      import('@/utils/Export2Excel.js').then(async (excel) => {
        const settleUnit = this.$route.query.settleUnit
        // 获取所有的数据
        const { list } = await request.getEquipmentConsumeSubDetails({
          page: 1,
          limit: this.page.total,
          settleUnit,
        })
        // list就是所有的数据，list [{},{},{}] 转换为 => [[],[],[],[]]
        const data = this.formatJson(this.headers, list) // 转化数据表结构
        const { combined } = combinedFn(Object.values(this.headers).slice(1), list, [
          'firstPartyAmount',
          'firstPartyCost',
          'noFirstPartyCost',
          'consumeDeduction',
          'shouldDeduction',
          'deduction',
          'noDeduction',
          'consumeNo',
          'deductionNo',
        ])
        data.push(['合计', ...Object.values(combined)])
        excel.export_json_to_excel({
          header: Object.keys(this.headers),
          data,
          filename: '设备管理-设备消耗扣款台账-' + this.orgName,
          autoWidth: true,
          bookType: 'xlsx',
          multiHeader,
          merges,
        })
      })
    },
    formatJson (headers, list) {
      return list.map((item) => {
        return Object.values(headers).map((value) => {
          return item[value]
        })
      })
    },
  },
}
</script>
<style lang="scss">

</style>
